package net.tribal.comic.entities

import java.util.Date;
import java.math.*

class Comic {
	
	//	Persistence Attributes
	String 	title
	String 	description
	String 	nameFile
	
	boolean moderationRequest 	= false
	boolean rateable   			= true
	boolean commentable 		= true
	boolean disabled 			= false
	
	Date 	dateCreated
	Date 	lastUpdated

	
	//	Not Persistence Attributes
	static transients = ['rate', 'pathFile']	
	
	//	DB Relations
	static 	belongsTo = [createdBy:	User]
	
	static 	hasMany = 	[page:		Page,
						 ratings:	Rate,
						 comments:	Comment,
						 tags:		Tag]

	
	//	DB Constraits
	static constraints = {
		title(blank:false, unique:true, maxSize:50)
		description(nullable:true, maxSize:1000)
        nameFile(blank:true, nullable:true)
	}

	
	//	Default order to sort
	//static mapping = { sort "name" }

	
	
	//	Other Methods
	String getRate() {
		//	We calculated the average scores
		Float p = 0
		if(ratings){
			ratings.each(){ p += it.rate }
			p = p / ratings.size()
		}
		return p
	}
	
	String getPathFile() {
		def p = ""
		if (nameFile == null || nameFile == "") {
			nameFile = "noneComic.png"
			return "../images/"
		}
		return "../uploads/comic/"
	}
}